package com.rabbitMq.testlate.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMqConfig {
    //消息交换机
    public final static String RABBITMQ_EXCHANGE_VALUE="RABBITMQ_EXCHANGE_VALUE";

    //消息路由
    public final static String RABBITMQ_ROUTER_VALUE="RABBITMQ_ROUTER_VALUE";

    //消息队列
    public final static String RABBITMQ_QUEUE_VALUE="RABBITMQ_QUEUE_VALUE";

    //死信队列
    public final static String DLX_QUEUE_VALUE="DLX_QUEUE_VALUE";

    //死信交换机
    public final static String DLX_EXCHANGE_VALUE="DLX_EXCHANGE_VALUE";

    //死信路由
    public final static String DLX_ROUTER_VALUE="DLX_ROUTER_VALUE";

    //队列配置部分
    @Bean
    public Queue rabbitMqQueue(){
        Map<String,Object> params=new HashMap<>();
        //设置消息过期时间
        params.put("x-message-ttl", 1000*10);
        //设置死信交换机
        params.put("x-dead-letter-exchange", DLX_EXCHANGE_VALUE);
        //设置死信 routing_key
        params.put("x-dead-letter-routing-key", DLX_ROUTER_VALUE);
        return new Queue(RABBITMQ_QUEUE_VALUE,true,false,false,params);
    }

    @Bean
    public Exchange rabbitMqExchange(){
        return new DirectExchange(RABBITMQ_EXCHANGE_VALUE,true,false);
    }

    @Bean
    public Binding rabbitMqBinding(){
        return BindingBuilder
                .bind(rabbitMqQueue())
                .to(rabbitMqExchange())
                .with(RABBITMQ_ROUTER_VALUE)
                .noargs();
    }

    //死信部分
    @Bean
    public Queue DlxQueue(){
        return new Queue(DLX_QUEUE_VALUE,true,false,false);
    }

    @Bean
    public Exchange DlxExchange(){
        return new DirectExchange(DLX_EXCHANGE_VALUE,true,false);
    }

    @Bean
    public Binding DlxBinding(){
        return BindingBuilder
                .bind(DlxQueue())
                .to(DlxExchange())
                .with(DLX_ROUTER_VALUE)
                .noargs();
    }
}
